package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Clazz;
import com.itheima.entity.ClazzsQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.mapper.ClazzsMapper;
import com.itheima.service.ClazzsService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description ClazzsServiceImpl
 * @Author 你的名字拼接或昵称
 * @Date 2025-09-06
 */
@Slf4j
@Service
public class ClazzsServiceImpl implements ClazzsService {
    @Autowired
    private ClazzsMapper clazzsMapper;

    @Override
    public PageBean page(ClazzsQueryParam clazzsQueryParam) {

        PageHelper.startPage(clazzsQueryParam.getPage(),clazzsQueryParam.getPageSize());
        //查询班级列表
        List<Clazz> clazzsList= clazzsMapper.list(clazzsQueryParam);
        LocalDate now =LocalDate.now();
        for(Clazz clazz: clazzsList){
            if (clazz.getBeginDate().isAfter(now)){
                clazz.setStatus("未开班");
            }else if (clazz.getBeginDate().isBefore(now) &&clazz.getEndDate().isAfter(now)){
                clazz.setStatus("在读");
            }else if(clazz.getEndDate().isBefore(now)){
                clazz.setStatus("已结课");
            }
        }

        // 封装分页结果
        Page p=(Page) clazzsList;
        return new PageBean(p.getTotal(),p.getResult());

    }

    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzsMapper.save(clazz);
    }

    @Override
    public Clazz getId(Integer id) {
        return clazzsMapper.getId(id);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
       clazzsMapper.update(clazz);
    }

    @Override
    public void delete(Integer id) {
    clazzsMapper.delete(id);
    }

    @Override
    public List<Clazz> getlist() {
        return clazzsMapper.getlist();
    }
}
